var invoiceList = [];
var searchInvoiceList = [];
var invoiceTemplate = 
	"<tr id='invoice_id'>" + 
		"<td><input type='checkbox' class='pull-left list-check' value=''></td>" + 
		"<td class='index_1'>fpdm</td>" +
		"<td class='index_2'>fphm</td>" + 
		"<td class='index_3'>kprq</td>" +
		"<td class='index_4'>jym</td>" +
		"<td class='index_5'>noTaxAmount</td>" +
		"<td>status</td>" +
		"<td><a class='pirobox_gall img-popup' data-rel='gallery' href='/ynrfinance/static/invoiceImage' title='fplx'>查看发票图片</a></td>" +
		"<td><a data-toggle='modal' href='#modalNarrower' onclick='clickModal(this)'>编辑发票</a></td>" +
		"<td>companyName</td>" +	
		"<td>createTime</td>" +
	"</tr>";
function clickModal(ele){
	var trEle = $(ele).parent().parent();
	var invoice_id = trEle.attr("id");
	var fpdm = $(trEle).find(".index_1");
	var fphm = $(trEle).find(".index_2");
	var kprq = $(trEle).find(".index_3");
	var check_code = $(trEle).find(".index_4");
	var noTaxAmount = $(trEle).find(".index_5");
	$("#invoice_id").val(invoice_id);
	$("#fpdm_").val(fpdm.text());
	$("#fphm_").val(fphm.text());
	$("#kprq_").val(kprq.text());
	$("#check_code_").val(check_code.text());
	$("#noTaxAmount_").val(noTaxAmount.text());
}
function initICheck() {
	$('input:checkbox:not([data-toggle="buttons"] input, .make-switch input), input:radio:not([data-toggle="buttons"] input)').iCheck({
		checkboxClass : 'icheckbox_minimal',
		radioClass : 'iradio_minimal',
		increaseArea : '20%' // optional
	});

	// Checkbox listing
	var parentCheck = $('.list-parent-check');
	var listCheck = $('.list-check');

	parentCheck.on('ifChecked', function() {
		$(this).closest('.list-container').find('.list-check').iCheck('check');
	});

	parentCheck.on('ifClicked', function() {
		$(this).closest('.list-container').find('.list-check')
				.iCheck('uncheck');
	});

	listCheck.on('ifChecked', function() {
		var parent = $(this).closest('.list-container').find(
				'.list-parent-check');
		var thisCheck = $(this).closest('.list-container').find('.list-check');
		var thisChecked = $(this).closest('.list-container').find(
				'.list-check:checked');

		if (thisCheck.length == thisChecked.length) {
			parent.iCheck('check');
		}
	});

	listCheck.on('ifUnchecked', function() {
		var parent = $(this).closest('.list-container').find(
				'.list-parent-check');
		parent.iCheck('uncheck');
	});

	listCheck.on('ifChanged', function() {
		var thisChecked = $(this).closest('.list-container').find(
				'.list-check:checked');
		var showon = $(this).closest('.list-container').find('.show-on');
		if (thisChecked.length > 0) {
			showon.show();
		} else {
			showon.hide();
		}
	});
}

function initPirobox(){
	jQuery.browser = {};
    (function () {
		jQuery.browser.msie = false;
		jQuery.browser.version = 0;
		if (navigator.userAgent.match(/MSIE ([0-9]+)\./)) {
		    jQuery.browser.msie = true;
		    jQuery.browser.version = RegExp.$1;
		}
    })();
    
    //Lightbox
    $().piroBox_ext({
		piro_speed : 700,
		bg_alpha : 0.5,
		piro_scroll : true
    });
}

function getFplxZh(fplx) {
	switch(fplx) {
	case "10":
		return "电子发票";
	case "01":
		return "专用发票";
	case "04":
		return "普通发票";
	}
}

function getFormatTime(time) {
	var date =  new Date(time);
    var y = 1900+date.getYear();
    var m = "0"+(date.getMonth()+1);
    var d = "0"+date.getDate();
    return y+"-"+m.substring(m.length-2,m.length)+"-"+d.substring(d.length-2,d.length);
}

function processData(data){
	var html = "";        		
	for(var index in data) {
		var invoiceItem = invoiceTemplate;
		var invoice = data[index];
		invoiceItem = invoiceItem.replace("invoice_id", invoice.id);
		invoiceItem = invoiceItem.replace("fpdm", invoice.fpdm);
		invoiceItem = invoiceItem.replace("fphm", invoice.fphm);
		invoiceItem = invoiceItem.replace("kprq", invoice.kprq);
		invoiceItem = invoiceItem.replace("jym", invoice.code);
		invoiceItem = invoiceItem.replace("noTaxAmount", invoice.goodsamount);
		invoiceItem = invoiceItem.replace("status", invoice.rec_status==0?"待识别":"识别失败");
		invoiceItem = invoiceItem.replace("companyName", invoice.name);
		invoiceItem = invoiceItem.replace("/invoiceImage", invoice.file_path);
		invoiceItem = invoiceItem.replace("fplx", invoice.fplxName);
		invoiceItem = invoiceItem.replace("createTime", getFormatTime(invoice.create_time));
		html = html + invoiceItem;
	}
	$("#invoice_list").html("");
	$("#invoice_list").html(html);
	initICheck();
}

function getPendingInvoice(){
	$.ajax({
        type: "POST",
        url: "/ynrfinance/getPendingInvoice",
        success: function (data) {
        	if(data==null || data==undefined || data.length==0){
        		$("#tips").text("当前没有待处理发票数据！");
        	} else {
        		processData(data);
        		invoiceList = data;
        		initPirobox();
        		$("#tips").hide();
        		$("#invoice_data").show();
        	}
        },
        error: function (data) {
        	$("#tips").text("服务器暂时无法提供服务，请联系系统管理员！");
        }
    });
}

getPendingInvoice()
var searchKey = "";
$(".message-search").bind("keypress",function(e){
	if(e.keyCode==13){
		searchKey = $(".message-search").val();
		console.log(searchKey);
		searchInvoiceList = [];
		for(var index in invoiceList){
			var invoice = invoiceList[index];
			if(invoice.fpdm != null && invoice.fpdm!="" && invoice.fpdm.indexOf(searchKey)!=-1) {
				searchInvoiceList.push(invoice);
				continue;
			}
			if(invoice.fphm != null && invoice.fphm!="" && invoice.fphm.indexOf(searchKey)!=-1) {
				searchInvoiceList.push(invoice);
				continue;
			}
			if(invoice.code != null && invoice.code!="" && invoice.code.indexOf(searchKey)!=-1) {
				searchInvoiceList.push(invoice);
				continue;
			}
			if(invoice.goodsamount != null && invoice.goodsamount!="" && invoice.goodsamount.indexOf(searchKey)!=-1) {
				searchInvoiceList.push(invoice);
				continue;
			}
			if(invoice.name != null && invoice.name!="" && invoice.name.indexOf(searchKey)!=-1) {
				searchInvoiceList.push(invoice);
				continue;
			}
			if(invoice.kprq != null && invoice.kprq!="" && invoice.kprq.indexOf(searchKey)!=-1) {
				searchInvoiceList.push(invoice);
				continue;
			}
		}
		processData(searchInvoiceList);
		initPirobox();
	}
});

function submit(){
	var fphm = $('#fphm_').val();
	var fpdm = $('#fpdm_').val();
	var kprq = $('#kprq_').val();
	var check_code = $('#check_code_').val();
	var noTaxAmount = $('#noTaxAmount_').val();
	var invoice_id = $('#invoice_id').val();
	if(fphm == '') {
		$('#fphm_').css('border-color', 'red'); return;
	}
	if(fpdm == '') {
		$('#fpdm_').css('border-color', 'red'); return;
	}
	if(kprq == '') {
		$('#kprq_').css('border-color', 'red'); return;
	}
	if(check_code == '' && noTaxAmount == '') {
		$('#check_code_').css('border-color', 'red'); 
		$('#noTaxAmount_').css('border-color', 'red'); 
		return;
	}
	$.post('/ynrfinance/updateInvoiceBasicInfo', 
		{
			fphm : fphm,
			fpdm : fpdm,
			kprq : kprq,
			checkCode : check_code,
			noTaxAmount : noTaxAmount,
			invoice_id : invoice_id
		},
		function(data) {
			if(data.errorCode == '0000') {
				window.location.reload();
			}
		}
	);
}

$('.show-on').click(function() {
	var idList = [];
	$(":checked").each(function() {
		var id = $(this).parent().parent().parent().attr('id');
		if(id != null && id != undefined) {
			idList.push(id);
		}
	});
	console.log(idList);
	$.post('/ynrfinance/deleteInvoice', {idList : JSON.stringify(idList)}, function(data) {
		if(data.errorCode == '0000') {
			window.location.reload();
		}
	});
});